<script setup lang="ts">
import { ref } from "vue";
import useHookRouter from "@/hooks/router";
const { $router } = useHookRouter();

import service from "@/apis";

import useUserStore from "@/pinia/user";
const userStore = useUserStore();

const formData = ref({
  account: "",
  password: "",
});

const form = ref();
const loading = ref(false);
const onSubmit = async () => {
  await form.value.validate();
  try {
    loading.value = true;
    const { data } = await service.loginApi(formData.value);
    userStore.setUserInfo(data);
    $router.replace({
      name: "CONTROL_DASHBOARD",
    });
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <div class="w-screen h-screen bg-[#181818] relative text-white">
    <el-form
      ref="form"
      :model="formData"
      label-width="0px"
      size="large"
      class="w-1/3 absolute top-[25vh] left-1/2 -translate-x-1/2"
    >
      <p class="w-full !text-[30px] text-center !pb-[20px]">系统登录</p>
      <el-row :gutter="20">
        <el-col>
          <el-form-item
            label=""
            prop="account"
            :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]"
          >
            <el-input v-model="formData.account" placeholder="请输入账号" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item
            label=""
            prop="password"
            :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]"
          >
            <el-input
              v-model="formData.password"
              type="password"
              show-password
              placeholder="请输入密码"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="" prop="password">
            <el-button
              type="primary"
              class="w-full"
              :loading="loading"
              @click="onSubmit"
              >登 录</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style lang="scss" scoped></style>
